<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef"
             style="width: fit-content;float: left"
             v-show="showSearch" label-width="68px">
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @keyup.enter.native="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入订单编号"
          clearable
          style="width: 350px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-card class="box-card" style="width: 270px;float: right;margin-right: 50px;">
      <div slot="header" class="clearfix">
        <span>{{queryParams.beginTime}} 至 {{queryParams.endTime}}</span>
      </div>
      <div class="text item">
        订单量:{{total}} 单
      </div>
      <div class="text item">
        利润: {{profit}} 元
      </div>
    </el-card>

    <el-row :gutter="10" class="mb8" style="clear: both">
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:user:export']">导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" :cell-style="cellStyle">
      <el-table-column type="expand" width="55" align="center">
        <template v-slot="scope">

          <!--用户信息面板-->
          <el-card shadow="never" body-style="userCardStyle" style="margin-bottom: 15px">
            <div slot="header">
              <span>用户信息</span>
              <el-avatar :src="scope.row.wxUserVo.avatar" style="float: right; padding: 3px 0"></el-avatar>
            </div>
            <el-form ref="userInfoRef" :model="scope.row.wxUserVo" label-width="80px">
              <el-form-item label="昵称">
                <el-input v-model="scope.row.wxUserVo.nickName" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="性别">
                <el-input v-bind:value="scope.row.wxUserVo.sex ===1?'男':'女'" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="登录ip">
                <el-input v-model="scope.row.wxUserVo.loginIp" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="登录日期">
                <el-input v-model="scope.row.wxUserVo.loginDate" :disabled="true"></el-input>
              </el-form-item>
            </el-form>
          </el-card>

          <!--地址信息面板-->
          <el-card shadow="never">
            <div slot="header">
              <span>地址信息</span>
            </div>
            <el-form ref="addressRef" :model="scope.row.wxAddress" label-width="80px">
              <el-form-item label="收件人">
                <el-input v-model="scope.row.wxAddress.receiver" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="联系电话">
                <el-input v-model="scope.row.wxAddress.telephone" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="具体地址">
                <el-input v-model="scope.row.wxAddress.detailAddress" :disabled="true"></el-input>
              </el-form-item>
            </el-form>
          </el-card>

        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="number" />
      <el-table-column label="买家" align="center" prop="wxUserVo.nickName" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="下单时间" align="center" prop="createTime"/>
      <el-table-column label="利润(元)" align="center" class-name="profit"class="profit" prop="profit" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/wx/order";
const date = new Date();
const beginTimeData = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+1;
const endTimeData = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
export default {
  name: "Order",
  data() {
    return {
      // 检索的时间范围
      dateRange:[beginTimeData,endTimeData],
      // 显示搜索条件
      showSearch: true,
      loading:true,
      // 总条数
      total: 0,
      // 【订单】表格数据
      orderList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: "",
        beginTime:beginTimeData,
        endTime:endTimeData
      },
      userCardStyle:{
        background:"pink !important",
        margin:"0px 0px 15px 0px"
      },
      // 利润
      profit:0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【订单】列表 */
    getList() {
      if(this.dateRange !== null && this.dateRange.length>0) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        let value = 0;
        response.rows.forEach(e=>{
           value += (e.goodsWinningPrice-e.goodsCostPrice);
        })
        this.profit = value;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [beginTimeData,endTimeData];
      this.queryParams.number = "";
      this.queryParams.beginTime = beginTimeData;
      this.queryParams.endTime = endTimeData;
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 5) {
        // 指定列号
        return 'font-size:25px;color:#67C23A;'
      } else {
        return ''
      }
    },
  }
};
</script>
<style scoped>
  .text {
    font-size: 20px;
    color: #E6A23C;
  }

  .item {
    margin-bottom: 18px;
  }
</style>
